import { reactive, ref, computed } from 'vue'
import { AxiosProxy, Operation } from './proxy.js'
// import { ElMessage } from 'element-plus'


export class Store {
  url = null

  proxy = null

  constructor(data) {
    this.vm = reactive({
      status: 0, // 模型通信状态,
      page: 1,
      count: 0,
      pageSize: 10,
      data
    })
  }
  async load(filter) {
    // this.controller?.abort()
    // this.controller = new AbortController()
    this.preOperation?.abort()

    // filter.delay = (filter.page || 1) * 1000 
    const operation = {
      type: "read",
      filter
    }
    return this.execute(operation)
  }

  async loadByPage(page) {
    return this.load({
      page
    })
  }

  async execute(operationConfig) {
    const operation = new Operation(operationConfig)
    this.vm.status = 1
    this.preOperation = operation
    await this.proxy.execute(operation)
    if (operation.status == 2) {
      this.successOperation = operation
      this.vm.status = 2
      this.vm.data = operation.data
      Object.assign(this.vm, operation.pageInfo)
    } else if (operation.status == 3) {
      this.errorOperation = operation
      this.vm.status = 3
    } else if (operation.status == 4) {
      // 请求取消
      // this.vm.status = 2
    }
  }

  retry() {
    if (!this.errorOperation) {
      this.refresh()
      return
    }
    // cloen 模式 --》 把reqest相关的信息clone出来即可 
    return this.execute({
      type: this.errorOperation.type,
      option: this.errorOperation?.option,
      params: this.errorOperation?.params,
    })
  }

  refresh() {
    if (!this.successOperation) {
      // this.reset()
      return
    }

    // cloen 模式 --》 把reqest相关的信息clone出来即可 
    return this.execute({
      type: this.successOperation.type,
      option: this.successOperation?.option,
      params: this.successOperation?.params,
    })
  }
  cancel() {
    // this.controller?.abort()
    // this.vm.status = 2
    // this.vm.page = this.prePage || 1
  }
  async removeById(id) {
    const Model = this.model
    if (!Model) return
    const model = new Model({ data: { id } })
    await model.delete()
  }
}



export class Model {
  constructor(option) {

    const defaultData = {
      status: 0,
      data: {},
    }
    const data = option?.data ? Object.assign(defaultData, option) : defaultData

    this.vm = reactive(data)
    this.initData = JSON.parse(JSON.stringify(data))
  }

  // 请求数据
  async load() {

    this.vm.status = 1

    const operation = {
      type: 'read',
    }

    // 本地执行器
    await this.execute(operation)
  }

  async delete() {

    this.vm.status = 1

    const operation = {
      type: 'delete',
      option: {
        id: this.vm.data.id
      }
    }

    // 本地执行器
    await this.execute(operation)

  }

  async execute(operationConfig) {

    const operation = new Operation(operationConfig)

    if (!Object.hasOwn(operation, 'status')) {

      if (operation.type === 'read') {
        // 取消上一个请求
        if (this.preOperation?.controller) this.preOperation.controller.abort()
        // 保存read类型的请求，用于下一个重复请求时取消
        this.preOperation = operation
      }

      this.vm.status = 1

      // 初始化数据
      // if(!operation?.notSave) this.vm.data = []

      await this.proxy.execute(operation)
    }

    if (operation.status === 2) {
      if (operation.type === 'read') {
        this.successOperation = operation
      }
      this.vm.status = 2
      if (!operation?.notSave) this.setData(operation.data?.rows || operation.data?.data)

      if (operation?.data?.total) {
        if (!this.vm?.pageSize) this.vm.pageSize = 10
        if (!this.vm?.currentPage) this.vm.currentPage = 1
        this.vm.total = operation.data.total
      }

    } else if (operation.status === 3) {
      this.errorOperation = operation
      this.vm.status = 3
      // ElMessage.error(operation.error || '加载失败')


    } else if (operation.status === 4) {
      // 取消请求

    } else if (operation.status === 0) {
      // 请求失败时刷新
      this.vm.status = 2
      this.setData(operation.data)
    }
  }


  setData(data) {
    Object.assign(this.vm.data, data)
  }

  // 重试
  retry() {
    if (!this.errorOperation) {
      this.refresh()
      return
    }
    this.execute({
      type: this.errorOperation.type,
      method: this.errorOperation.method,
      option: this.errorOperation?.option,
      data: this.errorOperation?.data,
      params: this.errorOperation?.params,
    })
  }

  // 刷新
  refresh() {
    if (!this.successOperation) {
      this.reset()
      return
    }
    this.execute({
      type: this.successOperation.type,
      method: this.successOperation.method,
      option: this.successOperation?.option,
      data: this.successOperation?.data,
      params: this.successOperation?.params,
    })
  }

  // 重置
  reset() {
    if (!this.successOperation) {
      this.execute(this.initData)
      return
    }
    this.execute(this.successOperation)
  }


}
